declare const WeixinJSBridge: any
/**
 * 获取链接参数
 */
export function getQueryVariable(variable: any) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
}

/**
 * 表格时间格式化
 */
export function formatDate(cellValue: any) {
  if (cellValue == null || cellValue == "") return "";
  var date = new Date(cellValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return year + '-' + month + '-' + day
}

//获取两天之间的天数
export function getDaysBetween(startDate: string, endDate: string) {
  const sDate = Date.parse(startDate)
  const eDate = Date.parse(endDate)
  if (sDate > eDate) {
    return -1
  }
  const days = (eDate - sDate) / (1 * 24 * 60 * 60 * 1000)
  return days
}

//关闭微信窗口
export function closeWindow() {
  WeixinJSBridge.call('closeWindow')
}

export function delayCloseWindow(second: number = 1000) {
  setTimeout(() => {
    closeWindow();
  }, second)
}

//input小数位
export function inputDigit(value: string, digit: number) {
  if (value.indexOf('.') > 0) {
    return value = value.slice(0, value.indexOf('.') + digit + 1)
  }
  return value
}

//是否手机
export function isPhone() {
  //获取浏览器navigator对象的userAgent属性（浏览器用于HTTP请求的用户代理头的值）
  var info = navigator.userAgent
  //通过正则表达式的test方法判断是否包含“Mobile”字符串
  var isPhone = /mobile/i.test(info)
  //如果包含“Mobile”（是手机设备）则返回true
  return isPhone
}


// 判断是否为微信浏览器
export function isWeixinBrowser() {
  let ua = navigator.userAgent.toLowerCase();
  return /micromessenger/.test(ua) ? true : false;
}

//添加千分位
export function priceFormat(num: any, n: any = null) {
  let symbol = ",";
  if (num === null) return num;
  if (typeof num !== 'number') throw new TypeError('num参数应该是一个number类型');
  if (n < 0) throw new Error('参数n不应该小于0');
  let hasDot = parseInt(num.toString()) != num;//这里检测num是否为小数，true表示小数
  let m = (n != undefined && n != null) ? n : 1;
  num = m == 0 ? num.toFixed(m) + '.' : hasDot ? (n ? num.toFixed(n) : num) : num.toFixed(m);
  symbol = symbol || ',';
  num = num.toString().replace(/(\d)(?=(\d{3})+\.)/g, function (match: any, p1: any, p2: any) {
    return p1 + symbol;
  });
  if (n == 0 || (!hasDot && !n)) {//如果n为0或者传入的num是整数并且没有指定整数的保留位数，则去掉前面操作中的小数位
    num = num.substring(0, num.indexOf('.'));
  }
  return num;
}